/**
 * 补充材料脚本
 */
const ajax = require('../../tool/ajax');
const login = require('../login/index');
const caseDetail = require('../component/caseDetail');//案件详情右侧按钮
const supplementMaterial = require('../component/supplementMaterial');//当事人被动补充材料
const screen = require('../component/screen');//截图
let pupConfig, page, config, filing, caseInfo;

/**
 * 开始任务
 * @param myPupConfig 框架配置信息
 * @param myCaseInfo 案件信息,详见component/caseFile.js
 */
const start = async (myPupConfig, myCaseInfo) => {
  pupConfig = myPupConfig;
  page = pupConfig.page;
  config = pupConfig.config.common;
  filing = pupConfig.config.filing;
  caseInfo = myCaseInfo;

  await step1();
  await step2();
}

/**
 * 步骤一：秘书发送补充材料通知
 */
const step1 = async () => {
  await login(pupConfig, caseInfo.secretaryId);//秘书登录
  await inputSupplementInfo();
}

/**
 * 步骤二：申请人补充材料
 */
const step2 = async () => {
  await login(pupConfig);//申请人登录
  await startSupplement();
}

/**
 * 输入补充材料信息
 */
const inputSupplementInfo = async () => {
  //秘书点击补充材料按钮
  await caseDetail(pupConfig, caseInfo.caseId, 0);

  /**
   * 输入秘书意见和补充材料名称
   */
  await page.waitFor('span textarea');
  //可下拉输入框
  let inputName = await page.$('#litigantCertificateType input');
  for (let item of filing.supplement.names) {
    await inputName.type(item);
    await inputName.press('Enter');
  }
  await page.type('span textarea', filing.supplement.opinion);

  await screen(page, '补充材料信息', 1);

  await page.click('.ant-modal-body button');
  //确认进入详情页
  await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
}

/**
 * 申请人补充材料
 */
const startSupplement = async () => {
  //申请人点击补充材料按钮
  await caseDetail(pupConfig, caseInfo.caseId, 1);
  await supplementMaterial(pupConfig);
}

module.exports = start;